package cn.loan.service.impl;

import cn.loan.dao.ByStagesMapper;
import cn.loan.dao.QuotaMapper;
import cn.loan.entity.ByStages;
import cn.loan.service.ByStagesService;
import cn.loan.service.QuotaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: 毛苏宇
 * @Description: You can do it!
 * @Date: Create in 11:48 2019/3/22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ByStagesServiceImpl implements ByStagesService {


    @Autowired
    private ByStagesMapper byStagesMapper;
    @Autowired
    private QuotaMapper quotaMapper;


    @Override
    public boolean addByStages(ByStages byStages) throws Exception {
        //插入借款信息
        final int addByStages = byStagesMapper.insertByStages(byStages);
        final int modifyBalance = quotaMapper.updateBalance(byStages.getUser_id(), byStages.getBs_loan());
        //两个都成功插入则返回true,否则false
        if (addByStages > 0 && modifyBalance > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public double getAmountPayable(int userId) throws Exception {
        return byStagesMapper.findSum(userId);
    }

    @Override
    public boolean getLoanExist(int userId) throws Exception {
        //大于0说明有借款记录并没有下款
        return byStagesMapper.findLoanExist(userId) > 0 ? true : false;
    }

    @Override
    public ByStages getLoanRecord(int userId) throws Exception {
        return byStagesMapper.findLoanRecord(userId);
    }

}
